%% The Czech Republic - consolidation scenario
% by IMF Institute for Capacity Development - Course on Macro-fiscal modeling and analysis

close all;
clear all;

%%%%%
%% READ THE MODEL AND PREPARE THE DATA
%%%%%

%% Read the model
readmodel;

%% Read the data from the database
d = dbload('data.csv');

%% Data range for the simulation
startsim = yy(2011);
endsim = yy(2030);
rng = startsim:endsim;

%%%%%
%% SIMULATE A PLAIN SCENARIO - no exogenous data
%%%%%

%% Simulate the scenario
%s = simulate(m,d,rng);

%%%%%
%% SIMULATE THE SCENARIO
%%%%%

%% Define the simulation plan
simplan = plan(m,rng);

%% Fix the debt target
simplan = exogenize(simplan,{'debt_tar_rat'},rng);
simplan = endogenize(simplan,{'shock_debt_tar'},rng);

%% Fix some domestic known variables for 2011 - 2012
% Total deficit (shock to deficit fiscal rule)
simplan = exogenize(simplan,{'def_rat'},yy(2011):yy(2011));
simplan = endogenize(simplan,{'shock_def_rat'},yy(2011):yy(2011));

% Inflation (cost-push shock)
simplan = exogenize(simplan,{'dl_cpi'},yy(2011):yy(2012));
simplan = endogenize(simplan,{'shock_dl_cpi'},yy(2011):yy(2012));

% Real GDP (demad shock)
simplan = exogenize(simplan,{'l_y'},yy(2011):yy(2011));
simplan = endogenize(simplan,{'shock_l_y_gap'},yy(2011):yy(2011));

%% Fix foreign variables for 2011-2014
% Nominal interest rate, inflation and output gap
simplan = exogenize(simplan,{'rsf','dl_p_eu','l_y_eu_gap'},yy(2011):yy(2014));
simplan = endogenize(simplan,{'shock_rrf_gap','shock_dl_p_eu','shock_l_y_eu_gap'},yy(2011):yy(2014));

%% Add shocks to the database
% VAT increase
%d.shock_dl_cpi(yy(2013)) = 0.5;
%d.shock_dl_cpi(yy(2014)) = 0.3;

% Negative (increase)shock to country and term premiums
%d.shock_prem(yy(2012)) = 1;
%d.shock_tprem3(yy(2012)) = 1.5;
%d.shock_tprem10(yy(2012)) = 2;

%% Simulate the scenario
s = simulate(m,d,rng,'plan',simplan);

%% Merge historical and simulated data
s = dbextend(d,s);

%%%%%
%% Make the report
%%%%%

x = report.new('Scenario','visible',true);

% Figures
sty = struct();
sty.line.linewidth = 1.5;
sty.line.linestyle = {'-';'--'};
sty.line.color = {'k';'r'};
sty.axes.box = 'on';
sty.legend.location = 'Best';


x.figure('Scenario 2011 - 2030','subplot',[3,3],'style',sty,'range',startsim-4:endsim,'dateformat','YYYY:P');

x.graph([s.debt_rat.comment ' and the Target'],'legend',false);
x.series('',[s.debt_rat s.debt_tar_rat]);
x.highlight('',startsim-4:startsim-1);

x.graph([s.def_rat.comment],'legend',false);
x.series('',[s.def_rat]);
x.highlight('',startsim-4:startsim-1);

x.graph(s.def_struct_rat.comment,'legend',false);
x.series('',[s.def_struct_rat]);
x.highlight('',startsim-4:startsim-1);

x.graph(s.fiscimp_rat.comment,'legend',false);
x.series('',[s.fiscimp_rat]);
x.highlight('',startsim-4:startsim-1);

x.graph(s.l_y_gap.comment,'legend',false);
x.series('',[s.l_y_gap]);
x.highlight('',startsim-4:startsim-1);

x.graph(s.dl_y.comment,'legend',false);
x.series('',[s.dl_y]);
x.highlight('',startsim-4:startsim-1);

x.graph(s.dl_cpi.comment,'legend',false);
x.series('',[s.dl_cpi]);
x.highlight('',startsim-4:startsim-1);

x.graph('Monetary Policy Rate (1Y)','legend',false);
x.series('',[s.rsg1]);
x.highlight('',startsim-4:startsim-1);

x.graph(s.dl_czk_eur.comment,'legend',false);
x.series('',[s.dl_czk_eur]);
x.highlight('',startsim-4:startsim-1);

x.pagebreak();

x.figure('Scenario 2011 - 2030','subplot',[3,3],'style',sty,'range',startsim-4:endsim,'dateformat','YYYY:P');

x.graph(s.rsg3.comment,'legend',false);
x.series('',[s.rsg3]);
x.highlight('',startsim-4:startsim-1);

x.graph(s.rsg10.comment,'legend',false);
x.series('',[s.rsg10]);
x.highlight('',startsim-4:startsim-1);

x.graph(s.rsg_fcy.comment,'legend',false);
x.series('',[s.rsg_fcy]);
x.highlight('',startsim-4:startsim-1);

x.graph(s.prem.comment,'legend',false);
x.series('',[s.prem]);
x.highlight('',startsim-4:startsim-1);

x.graph(s.tprem3.comment,'legend',false);
x.series('',[s.tprem3]);
x.highlight('',startsim-4:startsim-1);

x.graph(s.tprem10.comment,'legend',false);
x.series('',[s.tprem10]);
x.highlight('',startsim-4:startsim-1);

x.graph(s.lrr_tnd.comment,'legend',false);
x.series('',[s.lrr_tnd]);
x.highlight('',startsim-4:startsim-1);

x.graph([s.l_y_tnd.comment 'y-o-y'],'legend',false);
x.series('',[s.l_y_tnd - s.l_y_tnd{-1}]);
x.highlight('',startsim-4:startsim-1);

x.graph([s.l_z_tnd.comment 'y-o-y'],'legend',false);
x.series('',[s.l_z_tnd - s.l_z_tnd{-1}]);
x.highlight('',startsim-4:startsim-1);

x.figure('Scenario 2011 - 2030: Foreing Variables','subplot',[2,2],'style',sty,'range',startsim-4:endsim,'dateformat','YYYY:P');

x.graph(s.rsf.comment,'legend',false);
x.series('',[s.rsf]);
x.highlight('',startsim-4:startsim-1);

x.graph(s.dl_p_eu.comment,'legend',false);
x.series('',[s.dl_p_eu]);
x.highlight('',startsim-4:startsim-1);

x.graph(s.rrf_gap.comment,'legend',false);
x.series('',[s.rrf_gap]);
x.highlight('',startsim-4:startsim-1);

x.graph(s.l_y_eu_gap.comment,'legend',false);
x.series('',[s.l_y_eu_gap]);
x.highlight('',startsim-4:startsim-1);

x.publish('Scenario','display',false);